package yxy.game.pm2.active.command.levelpackage;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actgradepackage;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_等级礼包;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActLevelPackageInfoRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

@MessageType(proto = Command.ActLevelPackageInfo, classtype = ActiveMessage.ActLevelPackageInfoRequest.class)
public class CMD_Active_等级礼包_Info extends Command<ActLevelPackageInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_等级礼包_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActLevelPackageInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActLevelPackageInfoResponse.Builder response = ActiveMessage.ActLevelPackageInfoResponse.newBuilder();
		
		ActiveModule_等级礼包 am = ActiveModule_等级礼包.get(playercode,false);

		Map<String, actgradepackage> agpmap = ActDataCenter.getAllData(actgradepackage.class, playercode);
		PlayerBean pb = PlayerBean.get(playercode);
		
		for(actgradepackage data : agpmap.values()) {
			ActiveMessage.LevelPackage.Builder build = ActiveMessage.LevelPackage.newBuilder();
			build.setId(data.getId());
			build.setLevelcondition(data.getCondition());

			int canget = am.getDrawids().contains(data.getId()) ? canget = DrawStatus.hadget.getStatus() : pb.getLevel() >= data.getCondition() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			build.setCanget(canget);
		
			ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			build.addAllCurs(items);
			
			response.addLevelpackage(build.build());
		}
		
		send(messageSource, response, Result.Success);
	}
	
	private void send(MessageSource messageSource, ActiveMessage.ActLevelPackageInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
